package com.tandil.seo;

import utils.Constantes;
import utils.PersistenceData;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;
 

public class SmsReceiver extends BroadcastReceiver{

	private String msj;
	private String[] mensaje;
	private String from;

	@Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                if (!msgs[i].getMessageBody().toString().isEmpty()){
                	from = msgs[i].getOriginatingAddress().toString();
                	msj += msgs[i].getMessageBody().toString(); }
            }
            from = from.trim();
            if (from.equalsIgnoreCase("66736") ){
                if (msj.contains("Transaccion")){
                    mensaje = msj.split("-");
                    String SaldAux = mensaje[3];
                    SaldAux = SaldAux.replaceAll("Saldo: ", "");
                    SaldAux = SaldAux.trim();
                    PersistenceData.writeSaldo(context, SaldAux, Constantes.ARCHIVO_SALDO);
                    String dominio = mensaje[0];
                    if (dominio.contains("null")){
                        dominio = dominio.replace("null", "");
                    }
                    Intent intentin = new Intent(context, Confirmation_Screen.class);
                    intentin.putExtra("CONFIRMACION", "TRANSACCION");
                    intentin.putExtra("DOMINIO", dominio);
                    intentin.putExtra("INICIO", mensaje[1]);
                    intentin.putExtra("FIN", mensaje[2]);
                    intentin.putExtra("SALDO", mensaje[3]);
                    intentin.putExtra("TRANSACCION", mensaje[4]);
                    intentin.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    intentin.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intentin);
                }else if (msj.contains("Pago Fallido")){
                        Toast.makeText(context, "Pago Fallido", Toast.LENGTH_SHORT).show();
                      }else if (msj.contains("Registrada")){
                            msj.replaceAll("null", "");
                            String saldo = msj.split(":")[1];
                            saldo.replaceAll("null", "");
                            PersistenceData.writeSaldo(context, saldo, Constantes.ARCHIVO_SALDO);
                            Intent intentaso = new Intent(context, Confirmation_Screen.class);
                            intentaso.putExtra("CONFIRMACION", "TARJETA");
                            intentaso.putExtra("SALDO", saldo);
                            intentaso.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                            intentaso.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intentaso);
                            }else{
                                     Toast.makeText(context, msj.replace("null", ""), Toast.LENGTH_SHORT).show();
                                    }
            }

        }                         
    }
}